package com.zz.ecommerce.declareManage.declareDetailInsp;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.insp.db.declaration.TDeclareFormGoodsListInsp;
import com.zz.ecommerce.common.bean.insp.db.declaration.TDeclareFormHeadInsp;
import com.zz.ecommerce.common.bean.qdc.db.TContainerListQdc;
import com.zz.ecommerce.common.bean.qdc.db.TDeclformHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TDeclformTaskQdc;
import com.zz.ecommerce.common.bean.qdc.db.TGoodsListQdc;
import com.zz.ecommerce.common.constants.CommonConstants;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by admin on 2016/3/29.
 * <p/>
 * 申报清单详情Controller
 */
@Controller
@RequestMapping("/declareDetailInsp")
@Scope("prototype")
public class InspDeclareDetailController {

    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    @Autowired
    private QdcGoodsListDao qdcGoodsListDao;

    @Autowired
    private TDeclformTaskQdcDao taskQdcDao;

    @Autowired
    private TContainerListQdcDao tContainerListQdcDao;

    @Autowired
    private TDeclareFormHeadInspDao tDeclareFormHeadInspDao;

    @Autowired
    private TDeclareFormGoodsListInspDao tDeclareFormGoodsListInspDao;


    /**
     * 申报清单详情页跳转
     *
     * @return
     */
    @RequestMapping(value = "declareDetail/{id}", method = {RequestMethod.GET})
    public String declareDetail(@PathVariable("id") String id, ModelMap map, String flag) {
        map.addAttribute("id", id);
        map.addAttribute("flag", flag);

        TDeclareFormHeadInsp tDeclareFormHeadInsp = new TDeclareFormHeadInsp();
        tDeclareFormHeadInsp.setId(id);
        map.addAttribute("declHead", tDeclareFormHeadInspDao.query(TableConstants.T_DECLARE_FORM_HEAD_INSP, tDeclareFormHeadInsp));

        // TODO: 2016/4/6  fanshilei:根据flag判断跳详情页还是修改页
        if (StringUtils.isEmpty(flag))
            return "declareManage/declareDetailInsp/declare_head_detail";
        else
            return "declareManage/declareDetailInsp/declare_head_edit";
    }

    /**
     * 清单表体列表
     *
     * @param tDeclareFormGoodsListInsp
     * @param request
     * @return
     */
    @RequestMapping("/queryTGoodsListInspList")
    @ResponseBody
    public GridResult queryTGoodsListInspList(TDeclareFormGoodsListInsp tDeclareFormGoodsListInsp, GridRequest request) {
        return new GridResult(tDeclareFormGoodsListInspDao.page(TableConstants.T_DECLARE_FORM_GOODS_LIST_INSP, tDeclareFormGoodsListInsp, request.getStart(), request.getEnd(), request.getSortOrder()), request, tDeclareFormGoodsListInspDao.count(TableConstants.T_DECLARE_FORM_GOODS_LIST_INSP, tDeclareFormGoodsListInsp));
    }

    /**
     * 保存申报单表头信息
     *
     * @param tDeclformHeadQdc
     * @return
     */
    @RequestMapping(value = "/updateTDeclformHeadQdc", method = RequestMethod.POST)
    @ResponseBody
    public int updateTDeclformHeadQdc(TDeclformHeadQdc tDeclformHeadQdc) {
        return tDeclformHeadQdcDao.update(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc, " WHERE ID = '" + tDeclformHeadQdc.getId() + "'");
    }

    /**
     * 保存申报单表体信息
     *
     * @param tGoodsListQdc
     * @return
     */
    @RequestMapping(value = "/updateTGoodsListQdc", method = RequestMethod.POST)
    @ResponseBody
    public int updateTGoodsListQdc(TGoodsListQdc tGoodsListQdc) {
        return qdcGoodsListDao.update(TableConstants.T_GOODS_LIST_QDC, tGoodsListQdc, " WHERE ID = '" + tGoodsListQdc.getId() + "'");
    }

    /**
     * 删除申报单信息
     *
     * @param tDeclformHeadQdc
     * @return
     */
    @RequestMapping(value = "/daleteDeclFormQdc", method = RequestMethod.POST)
    @ResponseBody
    public boolean daleteDeclFormQdc(TDeclformHeadQdc tDeclformHeadQdc) {

        if (StringUtils.isNotEmpty(tDeclformHeadQdc.getId())) {

            TDeclformHeadQdc updateObj = new TDeclformHeadQdc();
            updateObj.setId(tDeclformHeadQdc.getId());
            updateObj.setAppStepId(CommonConstants.DECLARE_DELETE);
            updateObj.setStatus(CommonConstants.STATUS_DELETE);
            tDeclformHeadQdcDao.update(TableConstants.T_DECLFORM_HEAD_QDC, updateObj, " WHERE ID = '" + updateObj.getId() + "'");

            TDeclformTaskQdc taskQdc = new TDeclformTaskQdc();
            taskQdc.setId(tDeclformHeadQdc.getId());
            taskQdcDao.insert(TableConstants.T_DECLFORM_TASK_QDC, taskQdc);
        }

        return true;
    }

    /**
     * 加载清单表体详情页
     *
     * @param tDeclareFormGoodsListInsp
     * @param map
     * @return
     */
    @RequestMapping(value = "loadDeclareBodyDetail")
    public String loadDeclareBodyDetail(TDeclareFormGoodsListInsp tDeclareFormGoodsListInsp, ModelMap map) {
        if (StringUtils.isNotEmpty(tDeclareFormGoodsListInsp.getId())) {
            map.put("goods", tDeclareFormGoodsListInspDao.query(TableConstants.T_DECLARE_FORM_GOODS_LIST_INSP, tDeclareFormGoodsListInsp));
        }
        return "declareManage/declareDetailInsp/declare_body_detail";
    }

    /**
     * 加载清单表体编辑页
     *
     * @param goodsListQdc
     * @param map
     * @return
     */
    @RequestMapping(value = "loadDeclareBodyEdit")
    public String loadDeclareBodyEdit(TGoodsListQdc goodsListQdc, ModelMap map) {
        if (StringUtils.isNotEmpty(goodsListQdc.getId())) {
            map.put("goods", qdcGoodsListDao.query(TableConstants.T_GOODS_LIST_QDC, goodsListQdc));
        }
        return "declareManage/declareDetailQdc/declare_body_edit";
    }

    /**
     * 清单集装箱信息
     *
     * @param tContainerListQdc
     * @param request
     * @return
     */
    @RequestMapping("/queryHeadContainerList")
    @ResponseBody
    public GridResult queryHeadContainerList(TContainerListQdc tContainerListQdc, GridRequest request) {
        return new GridResult(tContainerListQdcDao.page(TableConstants.T_CONTAINER_LIST_QDC, tContainerListQdc, request.getStart(), request.getEnd(), request.getSortOrder()), request, tContainerListQdcDao.count(TableConstants.T_CONTAINER_LIST_QDC, tContainerListQdc));
    }

    /**
     * 加载清单集装箱详情页
     *
     * @param tContainerListQdc
     * @param map
     * @return
     */
    @RequestMapping(value = "loadDeclareContainerDetail")
    public String loadDeclareContainerDetail(TContainerListQdc tContainerListQdc, ModelMap map) {
        if (StringUtils.isNotEmpty(tContainerListQdc.getId())) {
            map.put("container", tContainerListQdcDao.query(TableConstants.T_CONTAINER_LIST_QDC, tContainerListQdc));
        }
        return "declareManage/declareDetailQdc/declare_container_detail";
    }

    /**
     * 加载清单集装箱编辑页
     *
     * @param tContainerListQdc
     * @param map
     * @return
     */
    @RequestMapping(value = "loadDeclareContainerEdit")
    public String loadDeclareContainerEdit(TContainerListQdc tContainerListQdc, ModelMap map) {
        if (StringUtils.isNotEmpty(tContainerListQdc.getId())) {
            map.put("container", tContainerListQdcDao.query(TableConstants.T_CONTAINER_LIST_QDC, tContainerListQdc));
        }
        return "declareManage/declareDetailQdc/declare_container_edit";
    }

    /**
     * 保存申报单集装箱信息
     *
     * @param tContainerListQdc
     * @return
     */
    @RequestMapping(value = "/updateContainerQdc", method = RequestMethod.POST)
    @ResponseBody
    public int updateContainerQdc(TContainerListQdc tContainerListQdc) {
        return tContainerListQdcDao.update(TableConstants.T_CONTAINER_LIST_QDC, tContainerListQdc, " WHERE ID = '" + tContainerListQdc.getId() + "'");
    }

}
